const app = getApp();
Page({
	data:{
    hasZan: false
	},
  onLoad: function (options) {
    var that = this;
    if (options.hasOwnProperty('back')) {
      that.setData({
        back: options.back
      })
    } else {
      that.setData({
        back: 0
      })
    }
    app.request(app.apiDomain + 'index.php?c=point&a=account', {
			user_id:options.user_id
		}, function (res) {
      if (res.data.status == '1') {
        if (res.data.data.self){
            wx.navigateTo({
              url: 'personal',
            });
        }
        that.setData({
          user: res.data.data,
          user_focus:res.data.data.focus
        });
        wx.setNavigationBarTitle({
          title: res.data.data.nickname +'的个人主页',
        });
      }
    });

    app.request(app.apiDomain + 'index.php?c=point&a=latestreward', {
			user_id:options.user_id
		}, function (res) {
      if (res.data.status == '1') {
        that.setData({
          reward: res.data.data,
          reward_likes: res.data.data ? res.data.data.likes : {},
          reward_like_num: res.data.data ? res.data.data.like_num :0,
          reward_like: res.data.data ? res.data.data.reward_like :false
        });
      }
    });

    app.request(app.apiDomain + 'index.php?c=user&a=latestdaily', {
			user_id:options.user_id
		}, function (res) {
      if (res.data.status == '1') {
        that.setData({
          daily: res.data.data
        });
      }
    });
		
		
		console.log('开始绘画',options.user_id)
		//画布  
		app.request(app.apiDomain + 'index.php?c=user&a=stepStat', {
			user_id:options.user_id
		}, function (res) {
				that.setData({
						can_list:res.data.data,
						step_num:res.data.data[0].step,
						ka_num:(res.data.data[0].step/20).toFixed(1)
				});
				
				// console.log(res.data.data)
				const context = wx.createCanvasContext('per_canvas');
				// const can_list = res.data.data;
				//处理
				wx.getSystemInfo({
						success(res) {
								var width = res.windowWidth*0.76;
								var height = width*0.4;
								var a_win = width/6;
								var a_hei = height/22000;
								var h_hie = height*0.97;
								// 
								var data=that.data.can_list;
								// console.log(data)
								context.lineWidth = 2;//线条的宽度
								context.strokeStyle = "#ffef87";//线条的颜色
								for(var i in data){
										if(parseFloat(data[i].step) < 20000){
											 var a = h_hie-(data[i].step * a_hei)
										}else{
											 var a = 5
										}
										context.lineTo(parseFloat(a_win*i)+(width/40),a);
								}
								context.stroke();
								
								//画圆
								for(var i in data){
										if(parseFloat(data[i].step) < 20000){
											var a = h_hie-(data[i].step * a_hei)
										}else{
											var a = 5
										}
										context.fillStyle="#FEDC01";
										context.beginPath();
										context.arc(parseFloat(a_win*i)+(width/40),a,3.5,0,Math.PI*2,true);
										context.closePath();
										context.fill();
								}
								context.draw()
								
						}
				})
		});
		
		
		
  },
  likesubmit: function (e) {
    console.log(e.detail);
    var id = e.detail.target.dataset.id;
    var action = e.detail.target.dataset.action;
    if(action=='like'){
      this.like(id,e.detail.formId);
    }else {
      this.unlike(id, e.detail.formId);
    }
  },
  like: function (id, form_id) {
    var that = this;
    app.request(app.apiDomain + 'index.php?c=user&a=like', {
			type:'reward',
			id:id,
			// form_id:form_id
		}, function (res) {
      if (res.data.status == '1') {
        that.setData({
          reward_like: 1,
          reward_like_num:res.data.data.like_num,
          reward_likes: res.data.data.like_users
        });
      }
    });
  },
  unlike: function (id,form_id) {
    var that = this;
    app.request(app.apiDomain + 'index.php?c=user&a=unlike', {
				type:'reward',
				id:id,
				// form_id:form_id
		}, function (res) {
      if (res.data.status == '1') {
        that.setData({
          reward_like: 0,
          reward_like_num: res.data.data.like_num,
          reward_likes: res.data.data.like_users
        });

      }
    });
  },
  turnZan (e) {
    var id = e.detail.target.dataset.userId;
    var that = this;
    app.request(app.apiDomain + 'index.php?c=user&a=like', {
      type: 'reward',
      id: id,
      back: that.data.back
      // form_id:form_id
    }, function (res) {
      if (res.data.status == '1') {
        that.setData({
          hasZan: true
        })
      }
    });
  },
  focus:function(e){
    var that = this;
    app.request(app.apiDomain + 'index.php?c=user&a=focus',{
			follow_user_id:e.target.dataset.userId, 
		}, function (res) {
      if (res.data.status == '1') {
        that.setData({
          user_focus: 1
        });
      }
    });
  },
  unfocus: function (e) {
    var that = this;
    app.request(app.apiDomain + 'index.php?c=user&a=unfocus', {
			follow_user_id:e.target.dataset.userId
		}, function (res) {
      if (res.data.status == '1') {
        that.setData({
          user_focus: 0
        });
      }
    });
  },
  return_back () {
    let pages = getCurrentPages()
    if (pages.length == 1) {
      wx.redirectTo({
        url: '../index/index',
      })
    } else {
      wx.navigateBack({
        
      })
    }
  },
  return_act() {
    wx.redirectTo({
      url: '../index/index',
    })
  },
  onShareAppMessage: function () {
    return app.onShareAppMessage();
  }  ,
	onReady(e) {
    app.SystemInfo(this)
	}
})



